import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class FileWriter {
	Document doc;
	DocumentBuilder docBuilder;
	DocumentBuilderFactory docFactory;
	String location;

public FileWriter(String pa){
	this.location = pa;
//	doc = getXML(location);
}
	public void createDocument(String path){
		DocumentBuilder docBuilder =null;
		DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
		try {
			docBuilder = docFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Document my_doc = createRoot(docBuilder);
		transform(my_doc, path);
	}
	
	public Document createRoot(DocumentBuilder dbuilt){
		Document doc = dbuilt.newDocument();
		Element rootElement = doc.createElement("dataset");
		doc.appendChild(rootElement);
		return doc;
	}
	
	public void createParent(Element root, String lon, String latt, String[][] wifis){
		Element data = doc.createElement("daten");
		root.appendChild(data);
		
		Attr attr1 = doc.createAttribute("long_cor");
		attr1.setValue(lon);
		data.setAttributeNode(attr1);
		
		Attr attr2 = doc.createAttribute("lat_cor");
		attr2.setValue(latt);
		data.setAttributeNode(attr2);
		
		createChilds(data, wifis);
		
	}
	
	public void createChilds(Element parent_ele, String[][] wifi){
		//Element netz = doc.createElement("wifi");
		//netz.appendChild(doc.createTextNode("some_wifi"));
		//parent_ele.appendChild(netz);
		for(int i =0; i <= wifi.length; i++){
			Element netz = doc.createElement("wifi");
			parent_ele.appendChild(netz);
			for(int j =0; j <= wifi[i].length; j++)
			createAttr(j, netz, wifi[i][j]);
		}
		transform(doc, location);
	}
	
	public void createAttr(int count, Element net, String attr){
		Attr at;
		switch(count){
		case 0: at = doc.createAttribute("att1"); 
				at.setValue(attr); net.setAttributeNode(at);
				break;
		case 1: at = doc.createAttribute("att2"); 
				at.setValue(attr); net.setAttributeNode(at);
				break;
		case 2: at = doc.createAttribute("att3"); 
				at.setValue(attr); net.setAttributeNode(at);
				break;
		case 3: at = doc.createAttribute("att4"); 
				at.setValue(attr); net.setAttributeNode(at);
				break;
		case 4: at = doc.createAttribute("att5"); 
				at.setValue(attr); net.setAttributeNode(at);
				break;
		case 5: at = doc.createAttribute("att6"); 
				at.setValue(attr); net.setAttributeNode(at);
				break;
		default: break;
		}
		
	}
	
	public void transform(Document document, String location){
		// write the content into xml file
					TransformerFactory transformerFactory = TransformerFactory.newInstance();
					Transformer transformer = null;
					try {
						transformer = transformerFactory.newTransformer();
					} catch (TransformerConfigurationException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					DOMSource source = new DOMSource(document);
					
					StreamResult result = new StreamResult(new File(location));
			 
					try {
						transformer.transform(source, result);
					} catch (TransformerException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			 
					System.out.println("File saved!");
	}
	
	public Document getXML(String location){
		//location = "C:/Users/hannes/Desktop/file.xml";
		Document read = null;
		File f = new File(location);
		if (f.exists()){
		DocumentBuilder builder = null;
		DocumentBuilderFactory docFac = DocumentBuilderFactory.newInstance();
        try {
        	try {
				builder = docFac.newDocumentBuilder();
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			read = builder.parse(f);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		else{
			createDocument(location);
			getXML(location);
		}
		
        return read;
		
	}
	
	public void rewrite_file(String location, String[] koords, String[][] datensatz){
		this.doc = this.getXML(location);
		Element root = doc.getDocumentElement();
		createParent(root, koords[0], koords[1], datensatz);
	}
	}


